package com.kingmed.kmss.common.vo;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingmed.kmss.common.utils.BeanCopyUtils;
import lombok.Data;

import java.util.List;

/**
 * 分页数据封装类
 */
@Data
public class CommonPage<T> {
    private Integer pageNo;
    private Integer pageSize;
    private Integer totalPage;
    private Long total;
    private List<T> list;

    /**
     * 将MyBatis Plus 分页结果转化为通用结果(千万不要直接把mybatis的page对象作为接口返回)
     */
    public static <T> CommonPage<T> restPage(Page<T> pageResult) {
        CommonPage<T> result = new CommonPage<>();
        result.setPageNo(Convert.toInt(pageResult.getCurrent()));
        result.setPageSize(Convert.toInt(pageResult.getSize()));
        result.setTotal(pageResult.getTotal());
        result.setTotalPage(Convert.toInt(pageResult.getPages()));
        result.setList(pageResult.getRecords());
        return result;
    }


    public static <T, V> CommonPage<V> convertPage(Page<T> pageResult, Class<V> clz) {
        CommonPage<V> result = new CommonPage<>();
        result.setPageNo(Convert.toInt(pageResult.getCurrent()));
        result.setPageSize(Convert.toInt(pageResult.getSize()));
        result.setTotal(pageResult.getTotal());
        result.setTotalPage(Convert.toInt(pageResult.getPages()));
        result.setList(BeanCopyUtils.copyList(pageResult.getRecords(), clz));
        return result;
    }
}
